package com.tim.file.controller;

import com.tim.controller.BaseContoller;
import com.tim.file.service.SqlLiteServer;
import com.tim.poi.excel.ExcelHelper;
import com.tim.poi.excel.modelbean.PoiListBean;
import com.utils.ApiSql;
import com.utils.VerifyUtil;
import com.utils.exceptinos.OutException;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author tim
 * @version 1.0
 * @date 2022/12/19 15:07
 */
@RestController
@RequestMapping("/excel")
public class ExcelController extends BaseContoller {
    @Autowired
    ApiSql apiSql;
    @Autowired
    SqlLiteServer sqlLiteServer;


    /**
     * 整合tim-db工具
     *
     * @return
     */
    @RequestMapping("/down/{groups}/{codes}")
    @ApiOperation(value = "下载excel sql")
    public void down(HttpServletRequest request, HttpServletResponse response,
                     @PathVariable(name = "groups") String groups, @PathVariable(name = "codes") String codes,
                     @RequestBody(required = false) Map<String, Object> param) throws Exception {
        synchronized (ExcelController.this) {
            Map<String, Object> itemByGroupsCodes = sqlLiteServer.getItemByGroupsCodes(groups, codes);
            if (VerifyUtil.isEmpty(itemByGroupsCodes)) {
                throw new OutException("groups+codes 组合队友的脚本不存在找不到脚本无法导出对应的数据");
            }
            String sqlContent = itemByGroupsCodes.get("sqlContent") + "";
            String paramKeys = itemByGroupsCodes.get("paramKeys") + "";
            //获取配置数据
            try {
                List<Map<String, Object>> listData = apiSql.getListBySql(param, paramKeys, sqlContent);
                if (VerifyUtil.isEmpty(listData)) {
                    throw new OutException("groups+codes 未查询到数据！！！！");
                }
                Map<String, Object> stringObjectMap = listData.get(0);
                String tTitles = "";
                Set<String> strings = stringObjectMap.keySet();
                for (String string : strings) {
                    tTitles += string + ",";
                }
                tTitles = VerifyUtil.endClear(tTitles);
                // 创建HSSFWorkbook对象
                SXSSFWorkbook wb = ExcelHelper.creator()
                        // PoiListBean 为导出格式逻辑类，不同模板使用不同bean进行逻辑封装
                        .addEBean(new PoiListBean())
                        .addData(listData, tTitles, tTitles)
                        .build();
                String fileName = groups + "_" + codes + "_" + VerifyUtil.getDate();
                ExcelHelper.outFile(response, wb, fileName);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OutException(e.getMessage());
            }
        }
    }
}
